package cn.marinda.totipotentwar.command;

import cn.marinda.totipotentwar.TotipotentWar;
import cn.marinda.totipotentwar.command.annotation.SubCommands;
import cn.marinda.totipotentwar.global.AbstractGui;
import cn.marinda.totipotentwar.global.impl.GuiImpl;
import cn.marinda.totipotentwar.runnable.GameRunnable;
import cn.marinda.totipotentwar.utils.GameUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;

import java.lang.reflect.Method;

public class Commands implements CommandExecutor {
    /**
     * Executes the given command, returning its success
     *
     * @param sender  Source of the command
     * @param command Command which was executed
     * @param label   Alias of the command which was used
     * @param args    Passed command arguments
     * @return true if a valid command, otherwise false
     */
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(sender instanceof Player){
            Player player = (Player)sender;

        }else{
            Bukkit.getConsoleSender().sendMessage(replaceColor("&c控制台不允许使用该命令。"));
        }
        try{
            for (Method method:getClass().getDeclaredMethods()){
                method.setAccessible(true);
                SubCommands sub=method.getDeclaredAnnotation(SubCommands.class);
                if(sub==null){
                    continue;
                }
                if(sub.name().equalsIgnoreCase(args[0])){
                    String[] args1=new String[args.length-1];
                    for (int i = 1; i < args.length; i++) {
                        args1[i-1]=args[i];
                    }
                    method.invoke(this,sender,args1);
                    return true;

                }
            }
        }catch (Exception ignored){ }
        sender.sendMessage("§c错误的指令，请用/twar help查看帮助");
        return true;
    }

    @SubCommands(name = "help",length = 0)
    public void help(CommandSender p,String[] args){
        p.sendMessage(replaceColor("&7&m--------&6&l[&a&l      TotipotentWar      &6&l]&7&m--------"));
        p.sendMessage(replaceColor("&a/twar 为主命"));
        p.sendMessage(replaceColor("&a/twar help 打开命令帮助"));
        p.sendMessage(replaceColor("&a/twar open game 主界面"));
        p.sendMessage(replaceColor("&a/twar open shop 打开点数商城"));

        p.sendMessage(replaceColor("&a/twar integral 打开积分帮助"));
        p.sendMessage(replaceColor("&a/twar points 打开点数帮助"));

        p.sendMessage(replaceColor("&a/twar points 打开点数帮助"));
        p.sendMessage(replaceColor("&a/twar reload 重载插件"));
        p.sendMessage(replaceColor("&a/twar integral look [玩家名] 查询指定玩家的积分"));
        p.sendMessage(replaceColor("&a/twar integral set [玩家名] [积分] 设置指定玩家的积分"));
        p.sendMessage(replaceColor("&a/twar integral reset [玩家名] 重置玩家的积分"));
        p.sendMessage(replaceColor("&a/twar integral take [玩家名] 减少指定玩家的积分"));
        p.sendMessage(replaceColor("&a/twar points look [玩家名] 查询指定玩家的点数"));
        p.sendMessage(replaceColor("&a/twar points set [玩家名] [积分] 设置指定玩家的点数"));
        p.sendMessage(replaceColor("&a/twar points reset [玩家名] 重置玩家的点数"));
        p.sendMessage(replaceColor("&a/twar points take [玩家名] 减少指定玩家的点数"));
    }

    /**
     * 打开菜单
     *
     * @param p    p
     * @param args arg游戏
     */
    @SubCommands(name = "open",length = 1)
    public void openMenu(CommandSender p,String[] args){
        String shopName = args[0];
        AbstractGui gui = GuiImpl.getGui(shopName);
        gui.openInventory((Player) p);
    }

    /**
     * 开始游戏
     *
     * @param p    p
     * @param args arg游戏
     */
    @SubCommands(name = "start",length = 0)
    public void startGame(CommandSender p,String[] args){
        Player player = (Player) p;
        if(!GameUtil.enableFlag){
            player.sendMessage(replaceColor("&a游戏开启完毕。"));
            GameUtil.enableFlag = true;
        }
    }

    /**
     * 加入游戏
     *
     * @param p    p
     * @param args arg游戏
     */
    @SubCommands(name = "join",length = 0)
    public void joinGame(CommandSender p,String[] args){
        Player player = (Player) p;
        //JoinGame
        GameUtil.onJoinGame(player);
    }

    /**
     * 离开游戏
     *
     * @param p    p
     * @param args arg游戏
     */
    @SubCommands(name = "leave",length = 0)
    public void leaveGame(CommandSender p,String[] args){
        Player player = (Player) p;
        GameUtil.onLeaveGame(player);
    }



    /**
     * 替换颜色
     *
     * @param color 颜色
     * @return {@link String}
     */
    private String replaceColor(String color){
        return color.replace("&","§");
    }
}
